﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BPCSharp;
using Microsoft.PointOfService;

namespace POS
{
    public partial class OpenTickets : DevExpress.XtraEditors.XtraUserControl
    {
        public static OpenTickets _main;
        List<Control> ControlList = new List<Control>();

        double subtotal = 0.00;
        double taxrate = 0.0825;
        double tax = 0.00;
        double autogratrate = 0.00;
        double autograt = 0.00;
        double grat = 0.00;
        public static double total = 0.00;
        double Discount = 0.00;
        public static double paidtotal = 0.00;

        int ReceiptNumber = 0;

        public OpenTickets()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            _main = this;

            ControlList.Add(listView1);
            ControlList.Add(listView2);
            ControlList.Add(labelControl1);
            ControlList.Add(labelControl2);
            ControlList.Add(labelControl10);
            ControlList.Add(labelControl11);
            ControlList.Add(labelControl12);
            ControlList.Add(labelControl13);
            ControlList.Add(labelControl15);
            ControlList.Add(labelControl3);
            ControlList.Add(labelControl4);
            ControlList.Add(labelControl5);
            ControlList.Add(labelControl6);
            ControlList.Add(labelControl7);
            ControlList.Add(lblReceiptNumber);
            ControlList.Add(labelControl9);
            ControlList.Add(lblAutoGratTotal);
            ControlList.Add(lblBillTotal);
            ControlList.Add(lblDiscountTotal);
            ControlList.Add(lblGratTotal);
            ControlList.Add(lblPaidTotal);
            ControlList.Add(lblServerName);
            ControlList.Add(lblSubTotal);
            ControlList.Add(lblTableName);
            ControlList.Add(lblTaxTotal);
            ControlList.Add(lblTimeSeated);

            foreach (Control item in ControlList)
            {
                item.BackColor = this.BackColor;
                item.ForeColor = this.ForeColor;
            }

            lblServerName.Text = StaticProperties.ServerName;
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {

                if (item["ReceiptNo"].ToString().Trim() == lblReceiptNumber.Text)
                {
                    string tax = lblTaxTotal.Text;
                    string tax1 = tax.Replace("$", "");

                    item["ReceiptTaxTotal"] = tax1;
                }
            }

            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
        }

        private void OpenTickets_Load(object sender, EventArgs e)
        {
            GetOpenTickets(StaticProperties.ServerID);
        }

        private void GetOpenTickets(int ServerID)
        {
            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {
                if (item["ReceiptServerID"].ToString().Trim() == ServerID.ToString())
                {
                    ListViewItem lvi = new ListViewItem((item["ReceiptNo"]).ToString());
                    lvi.SubItems.Add((item["ReceiptTableName"]).ToString());
                    string subtotal = (item["ReceiptSubTotal"]).ToString();
                    double subtotal1 = Convert.ToDouble(subtotal);
                    lvi.SubItems.Add(subtotal1.ToString("C"));

                    listView1.Items.Add(lvi);
                }
            }
        }

        private void btnListSelect_Click(object sender, EventArgs e)
        {
            listView2.Items.Clear();

            if (listView1.SelectedIndices.Count <= 0)
            {
                MessageBox.Show("Please Select a Receipt to Edit");
            }
            else
            {
                int intselectedindex = listView1.SelectedIndices[0];
                if (intselectedindex >= 0)
                {
                    String text = listView1.Items[intselectedindex].Text;
                    ReceiptNumber = Convert.ToInt32(text);


                    foreach (DataRow item in DataTableClass.ReceiptData.Rows)
                    {
                        if (item["ReceiptNo"].ToString().Trim() == ReceiptNumber.ToString())
                        {


                            string subtotal = (item["ReceiptItemTotal"]).ToString();
                            double subtotal1 = Convert.ToDouble(subtotal);


                            ListViewItem lvi = new ListViewItem((item["ReceiptQty"]).ToString().Trim());
                            lvi.SubItems.Add((item["ReceiptDescription"]).ToString().Trim());
                            lvi.SubItems.Add(subtotal1.ToString("C"));

                            listView2.Items.Add(lvi);

                        }
                    }

                    foreach (DataRow item in DataTableClass.Receipt.Rows)
                    {
                        if (item["ReceiptNo"].ToString().Trim() == ReceiptNumber.ToString())
                        {
                            string Autograt = item["ReceiptAutoGrat"].ToString().Trim();
                            double Autograt1 = Convert.ToDouble(Autograt);

                            autograt = Autograt1;
                            if(autograt != 0)
                            {
                                autogratrate = 0.18;
                            }

                            string Grat = item["ReceiptGrat"].ToString().Trim();
                            double Grat1 = Convert.ToDouble(Grat);

                            grat = Grat1;

                            string paid = item["ReceiptGrandTotal"].ToString().Trim();
                            double paid1 = Convert.ToDouble(paid);

                            paidtotal = paid1;

                        }
                    }
                    CalculateTicket(ReceiptNumber);
                }
            }
        }

        private double GetSubTotal(int ReceiptNo)
        {
            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {
                lblReceiptNumber.Text = ReceiptNumber.ToString();

                if (item["ReceiptNo"].ToString().Trim() == ReceiptNumber.ToString())
                {
                    lblTableName.Text = item["ReceiptTableName"].ToString().Trim();
                    string subtotal1 = (item["ReceiptSubTotal"]).ToString();
                    subtotal = Convert.ToDouble(subtotal1);
                }
            }
            return subtotal;
        }

        private void CalculateTicket(int ReceiptNo)
        {
            subtotal = GetSubTotal(ReceiptNo);
            //MessageBox.Show(subtotal.ToString("C"));
            lblSubTotal.Text = subtotal.ToString("C");

            tax = subtotal * taxrate;
            lblTaxTotal.Text = tax.ToString("C");

            autograt = (subtotal + tax) * autogratrate;
            if (autograt != 0.00)
            {
                lblAutoGratTotal.Text = autograt.ToString("C");
            }
            else
            {
                lblAutoGratTotal.Text = "";
            }

            if (Discount != 0)
            {
                lblDiscountTotal.Text = Discount.ToString("C");
            }
            else
            {
                lblDiscountTotal.Text = "";
            }

            total = (subtotal + tax + autograt - Discount);
            lblBillTotal.Text = total.ToString("C");

            if (paidtotal == 0)
            {
                paidtotal = total + grat;
            }
            if (grat != 0)
            {
                lblGratTotal.Text = grat.ToString("C");
                lblPaidTotal.Text = paidtotal.ToString("C");
            }
            else
            {
                lblGratTotal.Text = "";
                lblPaidTotal.Text = "";
            }
        }

        private void btnNumber_MouseClick(object sender, EventArgs e)
        {
            var button = (SimpleButton)sender;
            txtNumberEntry.Text += button.Text;
        }

        private void btnCLR_MouseClick(object sender, EventArgs e)
        {
            txtNumberEntry.Text = "";
        }

        private void btnAutoGrat_Click(object sender, EventArgs e)
        {
            autogratrate = 0.18;
            CalculateTicket(ReceiptNumber);

            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {

                if (item["ReceiptNo"].ToString().Trim() == lblReceiptNumber.Text)
                {

                    string autograt = lblAutoGratTotal.Text;
                    string autograt1 = autograt.Replace("$", "");

                    item["ReceiptAutoGrat"] = autograt1;
                }
            }
        }

        private void btnGrat_Click(object sender, EventArgs e)
        {
            grat = Convert.ToDouble(txtNumberEntry.Text);
            CalculateTicket(ReceiptNumber);

            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {

                if (item["ReceiptNo"].ToString().Trim() == lblReceiptNumber.Text)
                {

                    string Grat = lblGratTotal.Text;
                    string grat1 = Grat.Replace("$", "");

                    item["ReceiptGrat"] = grat1;

                    string Paid = lblPaidTotal.Text;
                    string Paid1 = Paid.Replace("$", "");

                    item["ReceiptGrandTotal"] = Paid1;
                }
            }
        }

        private void btnCustomerPayTotal_Click(object sender, EventArgs e)
        {
            paidtotal = Convert.ToDouble(txtNumberEntry.Text);
            grat = paidtotal - total;

            CalculateTicket(ReceiptNumber);

            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {
                ;

                if (item["ReceiptNo"].ToString().Trim() == lblReceiptNumber.Text)
                {
                    string Grat = lblGratTotal.Text;
                    string grat1 = Grat.Replace("$", "");

                    item["ReceiptGrat"] = grat1;

                    string Paid = lblPaidTotal.Text;
                    string Paid1 = Paid.Replace("$", "");

                    item["ReceiptGrandTotal"] = Paid1;

                    CreditCardPayment.ProcessCapture(Paid1, "100217989481");
                }

                
            }
        }

        private void btnPaymentType_MouseCLick(object sender, EventArgs e)
        {
            var button = (SimpleButton)sender;


            if (button.Text == "CASH")
            {
                button.ForeColor = Color.Yellow;
                btnCreditCard.Enabled = false;
                btnGrat.Enabled = true;
                btnCustomerPayTotal.Enabled = true;
            }
            else if (button.Text == "CREDIT CARD")
            {
                button.ForeColor = Color.Yellow;
                btnCash.Enabled = false;
                progressPanel1.BackColor = Color.RoyalBlue;
                progressPanel1.ForeColor = Color.White;
                progressPanel1.Visible = true;
                progressPanel1.Caption = "PLEASE SLIDE CARD";
                progressPanel1.ShowDescription = false;

                CreditCardPayment.PreAuthAmount = total;
                CreditCardPayment.RecieptTaxAmount = tax;
                

                //CreditCardPayment.AmountToProcess = total;
                try
                {
                    CreditCardPayment payment = new CreditCardPayment();
                    payment.ProcessAuth2();
                }
                catch(Exception E)
                {
                    MessageBox.Show(E.InnerException.ToString());
                }

                btnGrat.Enabled = true;
                btnCustomerPayTotal.Enabled = true;
            }
        }

        private void bntSaveTicket_Click(object sender, EventArgs e)
        {

            //DataTableClass.SendDataToSqlServer(DataTableClass.Receipt.TableName);
            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //ReceiptNumber

            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new OrderEntryScreen(ReceiptNumber));
        }


    }
}
